const secretKey = require('/utils/secretKey.js');
const publicFun = require('/utils/public.js');
import { getMiyao, userData, getAqiContent } from '/utils/request'

App({
  globalData: {
    secondCount: '',
    userInfo: null, //支付宝用户信息
    userid: '',//'889489', //655568 //888255
    usermiyao: '', //用户密钥
    userData: null, //用户信息
    authCode: '', //authCode码
    zfbUid: '', //获取公益三小时数据 2088022915906671
    cityid: 33, //城市id
    location: {},
    aqiObj: null,
    actiontypeid: -10000,
    systemInfo: null, //手机信息
    pagesize: 30,
    hc: '', //缓存
    menuid: 7, // 7自带杯 5是塑料
  },
  onLaunch(options) {
    // 第一次打开
    // this.hidetabbar();
    this.getUserId();
    this.getMobileInfo();
    this.getLocation();
  },

  getUserId() {
    var that = this;
    my.getStorage({
      key: 'userid',
      success: function(res) {
        that.globalData.userid = res.data
      },
    });
    my.getStorage({
      key: 'usermiyao',
      success: function(res) {
        that.globalData.usermiyao = res.data
      },
    });
    my.getStorage({
      key: 'userData',
      success: function(res) {
        that.globalData.userData = res.data
      },
    });
    my.getStorage({
      key: 'zfbUid',
      success: function(res) {
        that.globalData.zfbUid = res.data
      },
    });
  },
  getLocation() {
    let that = this;
    return new Promise(function (resolve, reject) {
      my.getLocation({
        type: 3,
        success (res) {
          that.globalData.location.lng = res.longitude; //经度
          that.globalData.location.lat = res.latitude; //纬度
          that.globalData.location.province = res.province; //省
          that.globalData.location.city = res.city; //市
          that.globalData.location.district = res.district; //区
          that.globalData.location.address = res.pois[0].address; // 地址 na name
          resolve(that.globalData.location);
        },
        fail(err) {
          if(err.error == 11 || err.error == 12 || err.error == 13){
            publicFun.showToast(err.message);
          }else{
            publicFun.showToast('定位失败');
          }
          that.globalData.location.lng = 0; //经度
          that.globalData.location.lat = 0; //纬度
          that.globalData.location.province = ''; //省
          that.globalData.location.city = ''; //市
          that.globalData.location.district = ''; //区
          that.globalData.location.address = '不显示位置'; // 地址 na name
          reject(err.message)
        }
      })
    })
  },
  getMobileInfo() { //获取手机信息
    let that = this;
    my.getSystemInfo({
      success: (res) => {
        that.globalData.systemInfo = res; 
        that.globalData.mobileInfo = res.platform === "ios" ? 2 : 1;
      }
    });
  },

  // 获取用户信息
  async getUserData() {
    try {
      var params  = {
        Uid: this.globalData.userid,
        sudl_userid : this.globalData.userid,
        sudl_usermiyao: this.globalData.usermiyao
      }
      const res = await userData(params)
      if (res.IsSuccess == 1) {
        let name = res.UserName ? res.UserName : res.name;
        let data = {
          Url: res.Url,
          name: res.UserName ? res.UserName : res.name,
          UserName: res.ngotype == 1 ? name : name+'@'+res.ngoname,
          Phone: res.phone ? res.phone : res.Phone,
          SunValue: res.SunValue,
          ngotype: res.ngotype, // type=1 用户名，type=2 用户名@隶属机构 type=3隶属机构志愿者
          ngoid: res.ngoid, //机构id
          ngoname: res.ngoname, //机构名字
          teamid: res.teamid, //社团id
        }
        my.setStorage({
          key: 'userData',
          data: data
        });
        this.globalData.userData = data; 
        if(res.isdeleted == 1){
          this.signOut();
        }
      }else{
        publicFun.showToast(res.M);
        this.signOut();
      }
    } catch (e) {
      publicFun.showToast('失败，请稍后再试')
    }
  },
  async getAqiDetail() {
    let location = this.globalData.location;
    try{
      let params  = {
        CityName: location.city,
        Lat: location.lat,
        Lng: location.lng,
      }
      const res = await getAqiContent(params)
      if (res.S == 1) {
        this.globalData.aqiObj = res;
      }else{
        publicFun.showToast(res.M ? res.M : '加载失败');
      }
    }catch(e){
      publicFun.showToast('失败，请稍后再试')
    }
  },

  async getSecondCount() {
    let that = this;
    try {
      var params  = {
        str: publicFun.getTimestamp(),
        miyao: '2wfjwefik-w0fwfjweofjweof',
      }
      const res = await getMiyao(params)
      if (res.S == 1) {
        that.globalData.secondCount = '|'+res.D+'|'+res.H+'|'+res.M+'|'+res.Se;
      }
    } catch (e) {
      publicFun.showToast('失败，请稍后再试')
    }
  },
  getMiYao() {
    let data = publicFun.getTimestamp()+this.globalData.secondCount;
    let miyao = secretKey.encrypt(data);
    return miyao;
  },
});
